//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using OpenLS.Core.Serialization;
using OpenLS.Drawing.Serialization;

namespace OpenLS.Drawing
{
    public class GraphicalObject : IOfficeXmlSerializable
    {
        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (var c = context.ReadAndSwitch("graphic", DrawingConstants.DrawingNamespaceUri, false))
            {
                using (var c2 = c.Read("graphicData"))
                {
                    Uri = c2.GetOptionalString("uri");
                    c2.PassAttributes();
                    switch (Uri)
                    {
                        case "http://schemas.openxmlformats.org/drawingml/2006/picture":
                            Picture pic = new Picture();
                            using (ReadContext c3 = c2.SwitchNamespace(Uri, false))
                            {
                                pic.ReadXml(c3);
                            }
                            _item = pic;
                            break;
                        default:
                            data.Clear();
                            while (c2.ReaderLocalName != null)
                            {
                                Debug.WriteLine(c2.ReaderLocalName);
                                data.Add(UnknownElementHelper.Read(c2));
                                Debug.WriteLine("-------------");
                            }
                            break;
                    }
                }
            } 
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (var c = context.WriteAndSwitch("graphic", DrawingConstants.DrawingNamespace))
            {

                using (var c2 = c.Write("graphicData"))
                {
                    if (Item is Picture)
                        Uri = @"http://schemas.openxmlformats.org/drawingml/2006/picture"; //\\ be smarter;
                    else
                    {
                        Contract.Assert(false, "not implemented");
                    }
                    c2.SetString("uri", Uri);
                    if (Item != null)
                    {
                        if (Uri == null)
                        {
                        }
                        var c3 = c2.Switch(new XmlNamespace("pic", Uri, false));
                        {
                            c3.WriteOptionalElement(Item);
                        }
                    }
                    foreach (var element in data)
                    {
                        c2.WriteElement(element);
                    }
                }
            }
        }

        private string Uri;
        readonly List<IOfficeXmlSerializable> data = new List<IOfficeXmlSerializable>();
        private IOfficeXmlSerializable _item;

        #endregion

        public IOfficeXmlSerializable Item
        {
            get { return _item; }
            set { _item = value; }
        }
    }
}